package com.bimbimbip.manager;

import java.io.File;
import java.util.ArrayList;

import android.widget.TextView;

import com.bimbimbip.explorer.FileItem;

public class FileItemManager {
	private FileItem[] listFileItems;

	public static ArrayList<String> nameFolder = new ArrayList<String>();
	private int level = 0;
	public static ArrayList<Integer> DEMO_NODES_2 = new ArrayList<Integer>();
//	public static ArrayList<Integer> lTest = new ArrayList<Integer>();

	public FileItemManager() {
		listFileItems = null;
		loadFile("/mnt", "mnt", false);
	}

	public FileItem[] getChild(String path, TextView textPath) {
		textPath.setText(path);
		File file = new File(path);
		String list[] = file.list();
		if (list == null) {
			listFileItems = new FileItem[1];
			listFileItems[0] = new FileItem(false, false,
					"(Directory is empty... Read only file system )", null,
					"Nothing here", "empty");
		} else {
			listFileItems = new FileItem[list.length];
			for (int i = 0; i < list.length; i++) {
				File a = new File(path + "/" + list[i]);
				listFileItems[i] = new FileItem(true, a.isFile(), a.getName(),
						path + "/" + a.getName(), getInfor(a), null);
			}
		}
		return listFileItems;
	}

	public String getParent(String path) {
		return new File(path).getParent();
	}

	public String getInfor(File file) {
		try {
			if (file.isFile()) {
				return "File size : " + file.length() + " b";
			} else {
				if (file.list() == null)
					return "Directory is empty";
				return "Items cont : " + file.list().length + ".";
			}
		} catch (Exception e) {
			return e.toString();
		}
	}

	public FileItem[] search(String parent, String match) {
		ArrayList<String> strs = new ArrayList<String>();
		File file = new File(parent);
		String list[] = file.list();
		if (list != null) {
			for (int i = 0; i < list.length; i++) {
				if (list[i].lastIndexOf(match) != -1)
					strs.add(list[i]);
			}
		}

		FileItem[] items = null;
		if (strs.size() == 0) {
			items = new FileItem[1];
			items[0] = new FileItem(false, false,
					"(Directory is empty... Read only file system )", null,
					"Nothing here", "empty");
		} else {
			items = new FileItem[strs.size()];
			for (int i = 0; i < strs.size(); i++) {
				File a = new File(parent + "/" + strs.get(i));
				items[i] = new FileItem(true, a.isFile(), a.getName(), parent
						+ "/" + a.getName(), this.getInfor(a), null);
			}
		}

		return items;
	}

	public void loadFileToSyncNode(int[] DEMO_NODES) {
		for (int i = 0; i < DEMO_NODES.length; i++)
			DEMO_NODES[i] = DEMO_NODES_2.get(i);
	}

	private void loadFile(String path, String nFolder, boolean end) {
		nameFolder.add(nFolder);
		DEMO_NODES_2.add(level);
		File file = new File(path);
		String list[] = file.list();
		if (list == null) {
//			lTest.add(0);// thuc su null
			level--;
			if (end == true)
				level--;
		} else {
			if (list.length <= 0) {
//				lTest.add(1);// ko null, co <= 0 con
				level--;
				if (end == true)
					level--;
			} else {
//				lTest.add(2);// ko null, co > 1 con.
				for (int i = 0; i < list.length; i++) {
					level++;
					loadFile(path + "/" + list[i], list[i], fileEnd(i, list.length));
				}
			}
		}
	}
	private boolean fileEnd(int i, int lengList){
		if (i == lengList - 1)
			return true;
		else return false;
	}
}
